﻿package net.wangit.support.action;

import java.awt.Color;

import javax.servlet.http.HttpServletResponse;

import net.wangit.context.ActionContext;
import net.wangit.framework.config.MimeSettings;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
/**
 * 单元数据转pdf文件，导出对应列表的本页数据。 
 * 
 * @author watson
 * Feb 25, 2010
 */
public class Unit2PdfAction extends Unit2ExcelAction {

	private static final Log log;
	private static Font CHINESE_FONT;

	public Unit2PdfAction() {
	}

	// 下载接口。
	protected void doDownload(ActionContext ac, HttpServletResponse response)
			throws Exception {
		Document document = new Document(PageSize.A4);
		String fileName = (new StringBuilder(String.valueOf(unitId))).append(
				".pdf").toString();
		String contentType = MimeSettings.getContentType("pdf");
		java.io.OutputStream out = getOutputStream(response, fileName,
				contentType);
		PdfWriter.getInstance(document, out);
		document.open();
		Table t = new Table(elements.size(), data.size());
		t.setAutoFillEmptyCells(true);
		t.setPadding(3F);
		t.setSpacing(0.0F);
		t.setBorder(0);
		float widths[] = new float[elements.size()];
		for (int i = 0; i < elements.size(); i++) {
			ElementComponent element = (ElementComponent) elements.get(i);
			int width = element.getTdWidth();
			int widthMode = element.getTdWidthMode();
			Cell c = new Cell(new Phrase(element.getName(), CHINESE_FONT));
			c.setVerticalAlignment(5);
			c.setHorizontalAlignment(1);
			if (width > 0 && widthMode == 1) {
				if (width <= 200)
					widths[i] = 0.1F;
				else if (width > 200 && width < 300)
					widths[i] = 0.12F;
				else if (width >= 300 && width < 400)
					widths[i] = 0.15F;
				else
					widths[i] = 0.2F;
			} else if (width < 0)
				widths[i] = 0.08F;
			else
				widths[i] = (float) width / 1000F;
			c.setBackgroundColor(Color.GRAY);
			c.setHeader(true);
			c.setLeading(4F);
			t.addCell(c);
		}

		t.endHeaders();
		t.setWidths(widths);
		t.setAlignment(1);
		if (data != null) {
			for (int i = 0; i < data.size(); i++) {
				net.wangit.Row dataRow = data.getRow(i);
				for (int j = 0; j < elements.size(); j++) {
					ElementComponent ec = (ElementComponent) elements.get(j);
					String value = ElementUtil.getValueOrDefault(ac, ec,
							dataRow);
					String text = ec.getText(ac, value);
					if (text == null)
						text = "";
					Cell c = new Cell(new Phrase(text, CHINESE_FONT));
					c.setVerticalAlignment(5);
					c.setHorizontalAlignment(1);
					c.setLeading(5F);
					t.addCell(c);
				}

			}

		}
		document.add(t);
		document.close();
	}

	static {
		log = LogFactory
				.getLog(net.wangit.support.action.Unit2PdfAction.class);
		try {
			CHINESE_FONT = new Font(BaseFont.createFont("STSongStd-Light",
					"UniGB-UCS2-H", false), 8F, 0, Color.black);
		} catch (Exception e) {
			log.error("Font error: STSongStd-Light/UniGB-UCS2-H");
		}
	}
}
